#ifndef HOCODEEDIT_H
#define HOCODEEDIT_H

#include <QTextEdit>
#include <QTextCursor>
#include <QScrollArea>
#include <QAbstractItemView>
#include <QScrollBar>
#include <QPainter>
#include <QAbstractTextDocumentLayout>
#include <QPlainTextEdit>
#include "hosyntaxhighlighter.h"
#include "hoautocompleter.h"

class HoCodeEdit;

class HoLineNumberWidget : public QWidget
{
    Q_OBJECT
public:
    explicit HoLineNumberWidget(HoCodeEdit *edit, QScrollArea* area = nullptr);
    int lineNumberAreaWidth() const;

protected:
    void paintEvent(QPaintEvent *event) override;

private:
    HoCodeEdit *codeEditor;
};

class HoCodeEdit : public QTextEdit
{
    Q_OBJECT

public:
    explicit HoCodeEdit(QStringList keywordPatterns, QWidget *parent = nullptr);
    QString getText() const;
    int lineNumberAreaWidth();
    QRectF blockBoundingGeometry(const QTextBlock &block) const;
    QPointF contentOffset() const;
    QRectF blockBoundingRect(const QTextBlock &block) const;
    int blockCount() const;
    QTextBlock firstVisibleBlock() const;
    void showLineNumber();
    void lineNumberAreaPaintEvent(QPaintEvent *event);

protected:
    void keyPressEvent(QKeyEvent *e) override;
    void resizeEvent(QResizeEvent *event) override;

public slots:
    void updateLineNumberAreaHeight();
    void updateLineNumberAreaWidth(int /* newBlockCount */);
    void highlightCurrentLine();
    void syncVScroll(int value);
    void syncHScroll(int value);

private:
    QString textUnderCursor() const;
    bool isShortcut(QKeyEvent *e) const;
    void insertCompletion(const QString &completion);
    HoSyntaxHighlighter *highlighter;
    HoAutoCompleter *completer;
    HoLineNumberWidget *lineNumberArea;
    QScrollArea *scrollArea;
};

#endif // HOCODEEDIT_H
